package csu.web.mypetstore.controller;

import csu.web.mypetstore.pojo.Product;
import csu.web.mypetstore.service.ProductService;
import csu.web.mypetstore.utils.Result;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RestController
@CrossOrigin
public class ProductController {
    @Autowired
    private  ProductService productService;
    // 获取产品信息
    @GetMapping(value = "/categories/{categoryId}/products/{productId}")
    public Result getProduct(@PathVariable("productId") String id) {
        Product product = productService.getProduct(id);
        return product != null ? Result.success(product) : Result.failure("产品未找到");
    }
    @GetMapping("/categories/{categoryId}/products")
    public Result getProductList(@PathVariable("categoryId") String categoryId){
        return Result.success(productService.getProductListByCategory(categoryId));
    }
    @GetMapping(value = "/categories/{categoryId}/products/similar")
    public Result searchProduct(@RequestParam("keyword") String keyword) {
        return productService.searchProduct(keyword);
    }
}
